package gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//class for panel, witch helps to create new map
public class NewMap extends MainPanel{

	private static final long serialVersionUID = 1L;
	private MediaTracker mt;
	private GameButton bBack, bOk;
	private Image img, imgBuf = null;
	private GameComboBox xDimComboBox, yDimComboBox;
	private int xPrev, yPrev, imageWidth, imageX;


	//inner class for back button's action listener
	public class ClickedOnBack implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			MainWindow.getMainWindow().switchToEditorMenu();
		}
		
	}

	//inner class for OK button's action listener
	public class ClickedOnOk implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			MainWindow.getMainWindow().switchToMapEditor();
		}
		
	}
	
	//When we are increasing the X value
	public class XIncrased implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if( (xPrev < Integer.parseInt(xDimComboBox.getSelectedItem().toString())) &&
					Integer.parseInt(xDimComboBox.getSelectedItem().toString()) > yPrev )
				yDimComboBox.setSelectedItem(Integer.parseInt(xDimComboBox.getSelectedItem().toString()));
			xPrev = Integer.parseInt(xDimComboBox.getSelectedItem().toString());
		}
		
	}
	
	//When we are reducing the X value
	public class YReduced implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if( yPrev > Integer.parseInt(yDimComboBox.getSelectedItem().toString()) ){
				if( Integer.parseInt(yDimComboBox.getSelectedItem().toString()) < xPrev )
					xDimComboBox.setSelectedItem(Integer.parseInt(yDimComboBox.getSelectedItem().toString()));
			}
			yPrev = Integer.parseInt(yDimComboBox.getSelectedItem().toString());
		}
		
	}
	
	//constructor of class
	public NewMap() {
		img = Toolkit.getDefaultToolkit().getImage("pic/mapsize.png");
		mt = new MediaTracker(this);
		mt.addImage(img, 0);
		try{
			mt.waitForAll();
		}catch(InterruptedException e){}
		imageWidth = 750 * MainWindow.getInnerHeight() / 600;
		imageX = (MainWindow.getInnerWidth() - imageWidth) / 2;
		xDimComboBox = new GameComboBox(getWidth()*(360+50)/800-50, getHeight()*(116+14)/600-14, 100, 28);
		yDimComboBox = new GameComboBox(getWidth()*(170+50)/800-50, getHeight()*(300+14)/600-14, 100, 28);
		for(int i = 1; i <= 100; i++){
			xDimComboBox.addItem(i);
			yDimComboBox.addItem(i);
		}
		xPrev = yPrev = 1;
		xDimComboBox.addActionListener(new XIncrased());
		yDimComboBox.addActionListener(new YReduced());
		bBack = new GameButton("Vissza", MainWindow.getInnerWidth() - 2 * (GameButton.DEFAULT_WIDTH + 10),
				MainWindow.getInnerHeight() - GameButton.DEFAULT_HEIGHT - 10);
		bBack.addActionListener(new ClickedOnBack());
		bOk = new GameButton("OK", MainWindow.getInnerWidth() - GameButton.DEFAULT_WIDTH - 10,
				MainWindow.getInnerHeight() - GameButton.DEFAULT_HEIGHT - 10);
		bOk.addActionListener(new ClickedOnOk());
		add(xDimComboBox);
		add(yDimComboBox);
		add(bBack);
		add(bOk);
	}
	
	//it paints the image as background
	public void paintComponent(Graphics g){
		if(imgBuf == null){
			imgBuf = createImage(getWidth(), getHeight());
			Graphics2D g2 = (Graphics2D)imgBuf.getGraphics();
			g2.setColor(getBackground());
			g2.fillRect(0, 0, imageX, MainWindow.getInnerHeight());
			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BICUBIC);
			g2.drawImage(img, imageX, 0, imageWidth, MainWindow.getInnerHeight(), null);
			g2.fillRect(imageX+imageWidth, 0, MainWindow.getInnerWidth(), MainWindow.getInnerHeight());
		}
		g.drawImage(imgBuf, 0, 0, null);
	}

}